#ifndef xpack_graphic_algo_binarization1
#define xpack_graphic_algo_binarization1
#pragma push_macro("xuser")
#undef  xuser
#define xuser mixc::graphic_algo_binarization1::inc
#include"define/base_type.hpp"
#include"macro/xalways_inline.hpp"
#include"macro/xexport.hpp"
#include"mixc.hpp"
#pragma pop_macro("xuser")

namespace mixc::graphic_algo_binarization1::origin{
    enum class binarization1_t{
        draw_white,
        draw_black,
    };

    struct binarization1_pack{
        uxx     x_start = 0;
        uxx     y_start = 0;
        uxx     width;
        uxx     height;
    };

    inline void xalways_inline binarization1(
        auto                    const & input, 
        binarization1_pack      const & pack,
        auto                    const & randerx
    ){
        auto width                      = pack.width;
        auto height                     = pack.height;
        auto x_start                    = pack.x_start;
        auto y_start                    = pack.y_start;
        auto x_end                      = uxx(x_start + width);
        auto y_end                      = uxx(y_start + height);

        // 算法描述:
        // 计算出一个像素的亮度 brightness，最大亮度 max_brightness 就是 red:255 * 3 + green:255 * 6 + blue:255 * 1 = 255 * 10
        // brightness / max_brightness 就是这个像素的亮度概率 p，把 input 的图像一维化，
        // 假设 p = 0.6，那么 pix0 = 0 pix1 = 0.6 pix2 = 1.2 pix3 = 1.8，那么 pix2 这个点由于积满 100% 的概率，
        // 如果某个点的概率是 0.6 那么它在 pix1 上可以绘制，否则不绘制，同理，后面 pix4 = pix3 + 0.6 = 2.4，这个点也是 p = 0.6 的绘制点
        // 判断是否是绘制点的方法比较简单，就是当前像素点 i 和后一个点 i + 1 在乘以概率 p 的圆整值是否不相等
        for(uxx y = y_start, i = 0; y < y_end; y += 1){
            for(uxx x = x_start; x < x_end; x += 1, i++){
                auto v                  = input[y][x];
                auto brightness         = uxx(v->red * 3 + v->green * 6 + v->blue * 1);

                constexpr
                auto max_brightness     = 255 * 10/*r:3+g:6+b:1*/;
                auto is_trigger         = i * brightness / max_brightness < (i + 1) * brightness / max_brightness;
                randerx(x, y, is_trigger ? binarization1_t::draw_white : binarization1_t::draw_black);
            }
        }
    }
}

#endif

xexport_space(mixc::graphic_algo_binarization1::origin)
